package com.cn.por.util.io;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * io框架介绍
 * 1、按照字节流与字符流进行分类
 * 1.1、字节流
 * 1.1.1、InputStream 输入流（基类），程序对文件进行读操作为输入流。
 * 		ByteArrayInputStream
 * 		FileInputStream
 * 1.1.2 OutputStream 输出流（基类），程序对文件进行写操作为输出流。
 * 1.2、
 * 
 * @author 26983
 *
 */
public class IoTest {
	/**
	 * 实现了文件的写操作。
	 */
	public static void fileOutputStream() {
		String content = "我是一个txt文件";
		byte[] contentByte =  content.getBytes();
		File txtFile = new File("文本.txt");
		System.out.println(txtFile.getAbsolutePath());
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream =new FileOutputStream(txtFile);
			fileOutputStream.write(contentByte);
			fileOutputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	/**
	 * 实现对文件的读操作
	 */
	public static void fileInputStream() {
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream("文本.txt");
			ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
			byte[] byteRead = new byte[1024];
			try {
				int length =0;
				while((length = fileInputStream.read(byteRead))!=-1) {
					byteOutputStream.write(byteRead, 0, length);
				}
				System.out.println(byteOutputStream.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	} 
	
	/**
	 * 使用输入字符流，实现对文件的读操作
	 */
	public static void inputStreamReader() {
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream("文本.txt");
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
			char[] buf = new char[2];
			try {
				int len=0;
				while((len=inputStreamReader.read(buf)) !=-1) {
					System.out.println(new String(buf,0,len));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	} 
	
	/**
	 * 通过类加载器获取文件
	 */
	public static void classLoader() {
		ClassLoader classLoader = IoTest.class.getClassLoader();
		InputStream inputStream = classLoader.getResourceAsStream("generator/classDesc.ftl");
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
		char[] buf = new char[1024];
		try {
			int len=0;
			while((len=inputStreamReader.read(buf)) !=-1) {
				System.out.println(new String(buf,0,len));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 读取jar包中的文件
	 */
	public static void jarFileReader() {
		ClassLoader classLoader = IoTest.class.getClassLoader();
		InputStream inputStream = classLoader.getResourceAsStream("org/apache/ibatis/builder/xml/mybatis-3-config.dtd");
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
		char[] buf = new char[1024];
		try {
			int len=0;
			while((len=inputStreamReader.read(buf)) !=-1) {
				System.out.println(new String(buf,0,len));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 对象转换为文件
	 */
	public static void objectOutputStream() {
		ObjectOutputStreamData o = new ObjectOutputStreamData();
		o.setAge("1");
		o.setName("黑无常");
		ObjectOutputStream oos = null;
		try {
			 oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
			 oos.writeObject(o);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 文件转换java对象
	 */
	public static void objectInputStream() {
		ObjectInputStream ois = null;
		InputStream is = null;
		try {
			is = new FileInputStream("person.txt");
			ois = new ObjectInputStream(is);
			try {
				ObjectOutputStreamData person = (ObjectOutputStreamData) ois.readObject();
				System.out.println(person.toString());
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				ois.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
	}
	
	
	public static void main(String[] args) {
		
		//fileOutputStream();
		//fileInputStream();
		//inputStreamReader();
		//classLoader();
		//jarFileReader();
		//objectOutputStream();
		//objectInputStream();
		jarFileReader();
	}

}